package com.fzrskj.cloud.free.miniservice.service;

import com.alibaba.fastjson.JSONObject;
import com.fzrskj.cloud.free.consts.CConsts;
import com.fzrskj.cloud.free.core.tool.CJsonTool;
import com.fzrskj.cloud.free.core.utils.EncryptUtils;
import com.fzrskj.cloud.free.sys.bean.SysMiniServiceBean;
import com.fzrskj.cloud.free.sys.utils.SysMiniServiceUtils;
import ms.core.consts.SysErr;
import ms.core.object.CObject;
import ms.core.tool.JsonTool;
import ms.core.tool.StrTool;
import ms.core.tool.XmlCodeParser;
import org.apache.commons.collections.map.HashedMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * MiniServiceService.
 *
 * @author zj.
 *         Created on 2019/5/22 0022.
 */
public class MiniServiceService extends CObject {
    /**
     * 微服务：添加微服务.
     */
    public String miniServiceAdd (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        //参数获取
        Integer serviceType = JsonTool.getIntValue(p, "serviceType", 0);
        String serviceCode = JsonTool.getStrValue(p, "serviceCode", "");
        String serviceName = JsonTool.getStrValue(p, "serviceName", "");
        String serviceApiUrl = JsonTool.getStrValue(p, "serviceApiUrl", "");
        String serviceApiSecretKey = JsonTool.getStrValue(p, "serviceApiSecretKey", "");
        String serviceManageUrl = JsonTool.getStrValue(p, "serviceManageUrl", "");
        String serviceEncryptionMethod = JsonTool.getStrValue(p, "serviceEncryptionMethod", "");
        String metadata = JsonTool.getStrValue(p, "metadata", "");
        Integer status = JsonTool.getIntValue(p, "status", -1);
        if (serviceType < 1 || serviceType > 2 || StrTool.isBlankStr(serviceCode) || StrTool.isBlankStr(serviceName) ||
                StrTool.isBlankStr(serviceApiUrl) || status < 0 || status > 1 || StrTool.isBlankStr(serviceApiSecretKey) ||
                StrTool.isBlankStr(serviceEncryptionMethod)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        serviceApiSecretKey = EncryptUtils.decodeSession(serviceApiSecretKey);
        if (StrTool.isBlankStr(serviceApiSecretKey)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        //是否已存在
        SysMiniServiceUtils miniServiceUtils = new SysMiniServiceUtils();
        SysMiniServiceBean serviceBean = miniServiceUtils.getByCode(serviceCode);
        if (null != serviceBean) {
            return XmlCodeParser.formatResultTips(false, "CODE EXISTS", "相同编码的服务已存");
        }

        //新增数据
        Map<String, Object> cdt = new HashedMap();
        cdt.put("serviceType", serviceType);
        cdt.put("serviceCode", serviceCode);
        cdt.put("serviceName", serviceName);
        cdt.put("serviceApiSecretKey", EncryptUtils.encodeSave(serviceApiSecretKey));
        cdt.put("serviceApiUrl", serviceApiUrl);
        cdt.put("metadata", metadata);
        cdt.put("status", status);
        cdt.put("onlineStatus", "on");
        cdt.put("serviceManageUrl", serviceManageUrl);
        cdt.put("serviceEncryptionMethod", serviceEncryptionMethod);

        //新增
        Integer affect = miniServiceUtils.newSysMiniService(cdt);
        if (affect < 1) {
            return XmlCodeParser.formatResultTips(false, miniServiceUtils.getErrCode(), miniServiceUtils.getMsg());
        }
        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 微服务：修改微服务.
     */
    public String miniServiceEdit(HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        //参数获取
        Integer id = JsonTool.getIntValue(p, "id", 0);
        Integer serviceType = JsonTool.getIntValue(p, "serviceType", 0);
        String serviceCode = JsonTool.getStrValue(p, "serviceCode", "");
        String serviceName = JsonTool.getStrValue(p, "serviceName", "");
        String serviceApiUrl = JsonTool.getStrValue(p, "serviceApiUrl", "");
        String serviceApiSecretKey = JsonTool.getStrValue(p, "serviceApiSecretKey", "");
        String serviceManageUrl = JsonTool.getStrValue(p, "serviceManageUrl", "");
        String metadata = JsonTool.getStrValue(p, "metadata", "");
        Integer status = JsonTool.getIntValue(p, "status", -1);
        String serviceEncryptionMethod = JsonTool.getStrValue(p, "serviceEncryptionMethod", "");
        if (id < 1 || serviceType < 1 || serviceType > 2 || StrTool.isBlankStr(serviceCode) ||
                StrTool.isBlankStr(serviceName) || StrTool.isBlankStr(serviceApiUrl) || status < 0 || status > 1 ||
                StrTool.isBlankStr(serviceApiSecretKey) || StrTool.isBlankStr(serviceEncryptionMethod)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }
        serviceApiSecretKey = EncryptUtils.decodeSession(serviceApiSecretKey);
        if (StrTool.isBlankStr(serviceApiSecretKey)) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        //是否已存在
        SysMiniServiceUtils miniServiceUtils = new SysMiniServiceUtils();
        SysMiniServiceBean serviceBean = miniServiceUtils.getByCodeWithoutId(serviceCode, id);
        if (null != serviceBean) {
            return XmlCodeParser.formatResultTips(false, "CODE EXISTS", "相同编码的服务已存");
        }

        //修改数据
        Map<String, Object> cdt = new HashedMap();
        cdt.put("id", id);
        Map<String, Object> update = new HashedMap();
        update.put("serviceType", serviceType);
        update.put("serviceCode", serviceCode);
        update.put("serviceName", serviceName);
        update.put("serviceApiSecretKey", EncryptUtils.encodeSave(serviceApiSecretKey));
        update.put("serviceApiUrl", serviceApiUrl);
        update.put("metadata", metadata);
        update.put("status", status);
        update.put("onlineStatus", "on");
        update.put("serviceManageUrl", serviceManageUrl);
        update.put("serviceEncryptionMethod", serviceEncryptionMethod);

        //修改
        Integer affect = miniServiceUtils.modSysMiniService(update, cdt);
        if (!SysErr.CODE_SUCCESS.equals(miniServiceUtils.getErrCode())) {
            return XmlCodeParser.formatResultTips(false, miniServiceUtils.getErrCode(), miniServiceUtils.getMsg());
        }
        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 微服务：获取微服务管理列表.
     */
    public String miniServiceQuery (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        //接收参数
        Integer page = CJsonTool.getIntValue(p, "page", 1);
        Integer pageSize = CJsonTool.getIntValue(p, "pagesize", CConsts.PAGE_SIZE);
        Integer serviceType = CJsonTool.getIntValue(p, "serviceType", 0);
        Integer status = CJsonTool.getIntValue(p, "status", -1);
        String serviceEncryptionMethod = CJsonTool.getStrValue(p, "serviceEncryptionMethod", "");
        String serviceName = CJsonTool.getStrValue(p,"serviceName","");
        //组装条件
        Map<String, Object> cdt = new HashedMap();
        if (status > -1) {
            cdt.put("status", status);
        }
        if (serviceType > 0) {
            cdt.put("serviceType", serviceType);
        }
        if (!StrTool.isBlankStr(serviceEncryptionMethod)) {
            cdt.put("serviceEncryptionMethod", serviceEncryptionMethod);
        }
        if(!StrTool.isBlankStr(serviceName)){
            cdt.put("serviceName",serviceName);
        }

        //查询
        SysMiniServiceUtils serviceUtils = new SysMiniServiceUtils();
        Integer rows = serviceUtils.querySysMiniService(cdt);
        List<SysMiniServiceBean> dataList = serviceUtils.querySysMiniService(cdt, "createTime desc", page, pageSize);
        if (null!=dataList && !dataList.isEmpty()) {
            for (SysMiniServiceBean serviceBean: dataList) {
                serviceBean.setServiceApiSecretKey(EncryptUtils.encodeSession(EncryptUtils.decodeSave(serviceBean.getServiceApiSecretKey())));
            }
        }
        Integer pages = Math.round((rows + pageSize - 1) / pageSize);

        return XmlCodeParser.formatResultPageData(SysErr.CODE_SUCCESS, page, pages, pageSize, rows, JSONObject.toJSONString(dataList));
    }

    /**
     * 微服务：删除微服务.
     */
    public String mimiServiceDelete (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        Integer id = JsonTool.getIntValue(p, "id", 0);
        if (id < 1) {
            return XmlCodeParser.formatResultTips(false, SysErr.SYS_PARAM_ERR);
        }

        SysMiniServiceUtils sysMiniServiceUtils = new SysMiniServiceUtils();
        Integer affect = sysMiniServiceUtils.delSysMiniService(id);
        if (affect < 1) {
            return XmlCodeParser.formatResultTips(false, sysMiniServiceUtils.getErrCode(), sysMiniServiceUtils.getMsg());
        }

        return XmlCodeParser.formatResultTips(true, SysErr.CODE_SUCCESS);
    }

    /**
     * 微服务：获取所有微服务.
     */
    public String mimiServiceSelect (HttpServletRequest request, HttpServletResponse response, String p, String sid) {
        //接收参数
        Integer status = CJsonTool.getIntValue(p, "status", -1);
        Integer serviceType = CJsonTool.getIntValue(p, "serviceType", -1);
        //组装条件
        Map<String, Object> cdt = new HashedMap();
        if (status > -1) {
            cdt.put("status", status);
        }
        if(serviceType > -1){
            cdt.put("serviceType",serviceType);
        }
        //查询
        SysMiniServiceUtils serviceUtils = new SysMiniServiceUtils();
        List<SysMiniServiceBean> dataList = serviceUtils.querySysMiniService(cdt, "createTime desc");
        if (null!=dataList && !dataList.isEmpty()) {
            for (SysMiniServiceBean serviceBean: dataList) {
                serviceBean.setServiceApiSecretKey(EncryptUtils.encodeSession(EncryptUtils.decodeSave(serviceBean.getServiceApiSecretKey())));
            }
        }

        return XmlCodeParser.formatResultData(true, SysErr.CODE_SUCCESS, JSONObject.toJSONString(dataList));
    }
}
